package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com 资历查询
 * 
 */
public class SearchZhiLi extends BaseJDBCPanel {
	BaseStore<BusiObject> store;
	PagingToolBar toolBar1 = null;
	private String bcodeStr = "0";
	private String nameStr = "0";
	private String typeStr = "0";

	Button search = new Button("查询");// 查询按钮

	public SearchZhiLi() {
		init();
	}

	public void init() {
		initWeigetx();
		registerMainStore(store);
		search.addListener(Events.OnClick, getListener());
	}

	private void initWeigetx() {
		toolBar1 = new PagingToolBar(15);

		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(5);
		FieldSet fs = new FieldSet();
		fs.setHeading("查询资历详情");

		fs.add(createForm());

		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.createGrid());
		panel2.setBottomComponent(toolBar1);

		vp.add(fs);
		vp.add(panel2);
		add(vp);
	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("name");
		type.getField("name").setType(String.class);
		type.addField("mark");
		type.getField("mark").setType(String.class);
		type.addField("type");
		type.getField("type").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_idCard");
		info.setKeyname(new String[] { "icode" });
		String sql = "select * from ( "
				+ "select bcode, cer as name, mark, '教育程度' as type from bcode_education "
				+ "union all "
				+ "select bcode, Qualifications, description,'资格' as type from bcode_quality "
				+ "union all "
				+ "select bcode, Skill, mark,'职称' as type from bcode_skill "
				+ ") a  where 1=2 ";
		info.setSql(sql);
		store = new BaseStore<BusiObject>(type, info);
		store.setToolBar(toolBar1);
		return store;
	}

	BaseGrid<BusiObject> grid = null;
	CheckBoxSelectionModel<BusiObject> cbs;

	BaseGrid<BusiObject> createGrid() {
		cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C1 = new ColumnConfig("bcode", "姓名", 120);
		BaseComboBoxForm<BusiObject> bf = new BaseComboBoxForm<BusiObject>(
				"bcode", "employno %2B bname", "v_bcode2job", "", true);
		C1.setRenderer(bf);
		bf.setAllowBlank(false);
		columns.add(C1);

		ColumnConfig C2 = new ColumnConfig("name", "资历名称", 140);
		columns.add(C2);

		ColumnConfig C3 = new ColumnConfig("mark", "资历备注", 140);
		columns.add(C3);

		ColumnConfig C7 = new ColumnConfig("type", "资历类型", 80);
		columns.add(C7);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, false, false);
		Tools.setResize(grid, 0.99, 0.70);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
		return grid;
	}

	FormPanel form;
	BaseComboBoxForm<BusiObject> bcode = new BaseComboBoxForm<BusiObject>(
			"bcode", "employno %2B bname", "v_bcode2job", "", true);
	TextField<String> name = new TextField<String>(); // 证件号码
	BaseComboBoxX type = new BaseComboBoxX("name", "dictionary",
			"where type='hr_zhili'");

	private FormPanel createForm() {
		form = new FormPanel();

		TableLayout tl = new TableLayout(8);

		form.setWidth(913);
		form.setPadding(4);
		form.setBodyBorder(false);
		form.setHeaderVisible(false);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		form.add(new Label("人员姓名:"), td);
		form.add(bcode, td);

		form.add(new Label("资历类型:"), td);
		form.add(type, td);

		form.add(new Label("资历名称:"), td);
		form.add(name, td);
		
		form.add(search);

		form.setLayout(tl);

		return form;
	}

	Listener<BaseEvent> defaultlistener;
	String searchSql = "";
	String sqlExcel = "";

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {

				String where = "where 1=1";
				if (be.getSource().equals(search)) {

					if (!bcode.getRawValue().equals("")) {
						bcodeStr = bcode.getValueCode();
						where += " and bcode = '" + bcodeStr + "'";
					} else {
						bcodeStr = "0";
					}

					if (!name.getRawValue().equals("")) {
						nameStr = name.getRawValue();
						where += " and name like '%25" + nameStr + "%25'";
					} else {
						nameStr = "0";
					}

					if (!type.getRawValue().equals("")) {
						typeStr = type.getRawValue();
						where += " and type = '" + typeStr + "'";
					} else {
						typeStr = "0";
					}

					String searchSql = "select * from ( "
							+ "select bcode, cer as name, mark, '教育程度' as type from bcode_education "
							+ "union all "
							+ "select bcode, Qualifications, description,'资格' as type from bcode_quality "
							+ "union all "
							+ "select bcode, Skill, mark,'职称' as type from bcode_skill "
							+ ") a " + where;

					grid.getStore().getStoreinfo().setSql(searchSql);
					grid.getStore().reload();
				} else {
				}
			}

		};
		return defaultlistener;
	}
}
